﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
using System.ComponentModel;
using JHoelsch.DataClases.Serialization;

namespace JHoelsch.DataClases.DataBase
{
    public sealed class DataHolder : INotifyPropertyChanged        
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }


        private static volatile DataHolder instance;
        private static object syncRoot = new Object();       
        internal string ProgDataPath { get; set; }
        public System.Collections.Specialized.StringCollection OpenCars {get;set;}
        private DataHolder()
        {
        }
        ~DataHolder()
        {
            XmlSerializationTool.writeToFile(Tankstellen, ProgDataPath + @"Tankstellen.xml");
        }
        public void initialiazeData(string progDataPath, System.Collections.Specialized.StringCollection cars)
        {
            OpenCars = cars;
            this.ProgDataPath = progDataPath;
            System.IO.Directory.CreateDirectory(ProgDataPath);
            this.Tankstellen = XmlSerializationTool.readFromFile<ObservableCollection<TankStelle>>(ProgDataPath + @"Tankstellen.xml");
            if (this.Tankstellen == null) this.Tankstellen = new ObservableCollection<TankStelle>();
            this.AutoDatenSaetze = new ObservableCollection<Car>();
            AutoDatenSaetze.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(AutoDatenSaetze_CollectionChanged);
            foreach (string filename in OpenCars)
            {
                if(System.IO.File.Exists(filename))
                    this.AutoDatenSaetze.Add(Car.load<Car>(filename));
            }
        }

        void AutoDatenSaetze_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("collection changed");
        }
        public static DataHolder Instance{
            get
            {
                if(instance == null)
                {
                    lock(syncRoot)
                    {
                        if(instance == null)
                            instance = new DataHolder();
                    }                
                }
                return instance;
            }
        }
        [XmlArray("Tankstellen")]
        public ObservableCollection<TankStelle> Tankstellen { get; set; }
        private ObservableCollection<Car> autoDatenSaetze = new ObservableCollection<Car>();
        public ObservableCollection<Car> AutoDatenSaetze { 
            get {return autoDatenSaetze;}
            set
            {
                autoDatenSaetze = AutoDatenSaetze;
                NotifyPropertyChanged("AutoDatenSaetze");
            }
        }

        public TankStelle addTankstelle(TankStelle toAdd)
        {
            if (Tankstellen.Contains(toAdd))
            {
                return toAdd;
            }
            else
            {
                if (Tankstellen.FirstOrDefault(obj => obj.Id == toAdd.Id) != null)
                {
                    return Tankstellen.FirstOrDefault(obj => obj.Id == toAdd.Id);
                }
                else if (Tankstellen.FirstOrDefault(obj => obj.Name.StartsWith(toAdd.Name, StringComparison.CurrentCultureIgnoreCase)) != null)
                {
                    return Tankstellen.FirstOrDefault(obj => obj.Name.StartsWith(toAdd.Name, StringComparison.CurrentCultureIgnoreCase));
                }
                else
                {
                    Tankstellen.Add(toAdd);
                    return toAdd;
                }
            }
        }
    }
}
